package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.*;
import connection.ServiceProxy;

/**
 * Servlet implementation class ViewOtherAdvertisement
 */
public class ViewOtherAdvertisement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewOtherAdvertisement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getOtherAdvertisement(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getOtherAdvertisement(request,response);
	}
	
	private void getOtherAdvertisement(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String message = null; 
		HttpSession session = request.getSession();
		String userId = (String)session.getAttribute("userId");
		
		ServiceProxy oProxy = new ServiceProxy();
		Advertisement aoAdvert[] = oProxy.getOtherAdvertisement(userId);
		if(null == aoAdvert) {
			System.out.println("No adds");
			message = "No Advertisement Listed";
			request.setAttribute("message", message);
		}
		else {
		//System.out.println(aoAdvert.length);
			//System.out.println("harry" + aoAdvert[0].getItemName());
			request.setAttribute("Advertisement", aoAdvert);
			
			CartItem aoCartItem[] = oProxy.getUserCart(userId);
			if(null != aoCartItem) {
				System.out.println("mes" + message);
				request.setAttribute("CartItem", aoCartItem);
			}
			
		}
		System.out.println("mes" + message);
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/viewOtherAdvertisement.jsp");
		rd.forward(request, response);
	}

}
